<?php



class ViewController extends Forum_Controller_Action
{
	public function groupAction() 
    {
    	if($this->_getParam('id')){
					$res = Forum::getSingleton('Forum_Group')->getById($this->_getParam('id'));
					if($res == null){ $this->render(); return;  }
				
				$this->view->forumGroup = $res;
	
					$res = Forum::getSingleton('Forum')->getListGroup($this->view->forumGroup['id']);	
					if( !count($res) ){ $this->render(); return;}
			
					$arrayForumId = Array();
					for($i = 0; $i < count($res); $i++){
						$arrayForumId[] = $res[$i]['id'];
				    }
				$this->view->forums = $res;    		
	
					$res = Forum::getSingleton('Forum_Thread')->getListCountById($arrayForumId);
					if( !count($res) ){ $this->render(); return;}
					
					$arrayForumThreads = Array();
					for($i = 0; $i < count($res); $i++){
				        $arrayForumThreads[$res[$i]['forum_id']] = $res[$i]['count'];
				    }
				$this->view->ForumThreads = $arrayForumThreads;		
			
					$res = Forum::getSingleton('Forum_Message')->getListCountInForumById($arrayForumId);
					if( !count($res) ){ $this->render(); return;}
					
					$arrayForumMessages = Array();
					for($i = 0; $i < count($res); $i++){
				        $arrayForumMessages[$res[$i]['forum_id']] = $res[$i]['count'];
				    }
				$this->view->ForumMessages = $arrayForumMessages;			
		
					$res = Forum::getSingleton('Forum_Message')->getListLastInForumById($arrayForumId);
					if( !count($res) ){ $this->render(); return;}
					
					$arrayForumLastMessages = Array();
					for($i = 0; $i < count($res); $i++){
				        $arrayForumLastMessages[$res[$i]['forum_id']] = $res[$i];
						if($res[$i]['user_id'] != null){
							$arrayUserId[] = $res[$i]['user_id'];							
						}
				    }
				$this->view->forumLastMessages = $arrayForumLastMessages;
					
					if( !isset($arrayUserId) ){ $this->render(); return;}
					$res = Forum::getSingleton('user')->getListById($arrayUserId);
					if( !count($res) ){ $this->render(); return;}
			        
					for($i = 0; $i < count($res); $i++){
			              $arrayUser[$res[$i]['id']] = $res[$i];
			        }
		        $this->view->users = $arrayUser; 
    	}
		$this->render();
	}
	
	public function forumAction() 
    {
   	if($this->_getParam('id')){
	
					$res = Forum::getSingleton('Forum')->getById($this->_getParam('id'));	
					if( !count($res) ){ $this->render(); return;}
					    
				$this->view->forum = $res;    		
	
					$res = Forum::getSingleton('Forum_Thread')->getListInForum($res['id']);
					if( !count($res) ){ $this->render(); return;}
				
					$arrayForumThreads = Array();
					$arrayThreadId = Array();
					for($i = 0; $i < count($res); $i++){
				        $arrayForumThreads[$res[$i]['id']] = $res[$i];
						$arrayThreadId[] = $res[$i]['id'];
				    }
				$this->view->ForumThreads = $arrayForumThreads;		
	 				
					$res = Forum::getSingleton('Forum_Message')->getListCountInThreadById($arrayThreadId);
					$arrayCountMessage = Array();
					for($i = 0; $i < count($res); $i++){
						$arrayCountMessage[$res[$i]['thread_id']] = $res[$i]['count'];
					}
				$this->view->ThreadsMessages = $arrayCountMessage;
					
					$res = Forum::getSingleton('Forum_Message')->getListLastInThreadById($arrayThreadId);
					$arrayLastMessage = Array();
					$arrayUserId = Array();
					for($i = 0; $i < count($res); $i++){
						$arrayLastMessage[$res[$i]['thread_id']] = $res[$i];
						if($res[$i]['user_id'] != null){
							$arrayUserId[] = $res[$i]['user_id'];							
						}
					}
				$this->view->ThreadsLastMessages = $arrayLastMessage;
					
					if( !isset($arrayUserId) ){ $this->render(); return;}
					$res = Forum::getSingleton('user')->getListById($arrayUserId);
					if( !count($res) ){ $this->render(); return;}
			        
					for($i = 0; $i < count($res); $i++){
			              $arrayUser[$res[$i]['id']] = $res[$i];
			        }
		        $this->view->users = $arrayUser;
				
    	}
		$this->render();   	
	}
	
	public function threadAction()
    {
    	if($this->_getParam('id')){
	    		$res = Forum::getSingleton('Forum_Thread')->getById($this->_getParam('id'));
				if($res == null){ $this->render(); return;	}
			$this->view->thread = $res;
			
				if($this->_getParam('page')){ $page = $this->_getParam('page');}else{ $page = 1;}
				$res = Forum::getSingleton('Forum_Message')->getPageByThreadId($res['id'], $page);
				if($res == null){ $this->render(); return;	}
			$this->view->messages = $res['list'];
			$this->view->totalPage = $res['total'];
			$this->view->page = $page;
					$arrayUserId = Array();
					for($i = 0; $i < count($res['list']); $i++){
						if($res['list'][$i]['user_id'] != null){
							$arrayUserId[] = $res['list'][$i]['user_id'];
						}	
					}
					if( !isset($arrayUserId) ){ $this->render(); return;}
					$res = Forum::getSingleton('user')->getListById($arrayUserId);
					if( !count($res) ){ $this->render(); return;}
			        
					for($i = 0; $i < count($res); $i++){
			              $arrayUser[$res[$i]['id']] = $res[$i];
			        }

		    $this->view->users = $arrayUser;
		
		}
		$this->render(); 
	}
}

?>